package com.ytkj.digitalConstruction.controller.constructionprocess;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ytkj.digitalConstruction.ReturnCode;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionBase;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionSucceed;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.reqbean.HistoryQueryBean;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.WholeFlowInfo;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionBaseService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionSucceedService;
import com.ytkj.digitalConstruction.service.constructionprocess.ConstructionUtilService;

/**
 * 不停航施工基本信息Controller
 * @author 宁煜
 *
 */
@RestController
@RequestMapping("constructionbase")
public class ConstructionBaseController {

	private static final Logger log = LoggerFactory.getLogger(ConstructionBaseController.class);

	
	@Autowired
	private ConstructionBaseService constructionBaseService;
	
	@Autowired
	private ConstructionSucceedService constructionSucceedService;
	
	/**
	 * 在指定项目下新增流程，返回项目子流程信息、项目信息与总体流程信息
	 *
	 * @param constructionBase 项目编号
	 * @param request 从request中获取web-token
	 * @param 
	 * @return ReturnCode
	 * @throws
	 */
	@PostMapping("newflow")
	public ReturnCode addNewFlow(@RequestBody @Valid ConstructionBase constructionBase, BindingResult bindingResult, HttpServletRequest request){
		if(bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error("新增流程参数异常："+msg);
			return new ReturnCode.Builder()
					.Success(false).Msg(msg).build();
		}
		
		//判断是不是施工单位（只有施工单位可以新建流程）
		boolean isExePersons = constructionBaseService.checkExePersons(request);
		if(!isExePersons) {
			return new ReturnCode.Builder().Success(false).Msg("只有施工单位可以新建流程！").build();
		}
		return new ReturnCode.Builder()
				.Object(constructionBaseService.addNewFlow(constructionBase, ConstructionUtilService.getUserByRequest(request)))
				.build();
	}
	
	/**
	 * @Description: 查询项目子流程全部进度信息
	 * @Title: showWholeFlowByFlowId 
	 * @param @param flowId
	 * @param @return 
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("wholeflow/show/{id}")
	public ReturnCode showWholeFlowByFlowId(@PathVariable("id") Integer flowId){
		WholeFlowInfo wholeFlowInfo = constructionBaseService.showWholeFlowByFlowId(flowId); 
		// 添加节点经办人名字
		constructionBaseService.setOperator(wholeFlowInfo);
		return new ReturnCode.Builder()
				.Object(wholeFlowInfo)
				.build();
	}
	
	/**
	 * @Description: 根据projectId查询整个流程信息，创建时间降序排列
	 * @Title: showWholeFlowByProjectId 
	 * @param @param projectId
	 * @param @return    设定文件 
	 * @return ReturnCode    返回类型 
	 * @throws
	 */
	@GetMapping("wholeflows/show/{projectId}")
	public ReturnCode showWholeFlowByProjectId(@PathVariable("projectId") Integer projectId) {
		return new ReturnCode.Builder()
				.Object(constructionBaseService.showWholeFlowByProjectId(projectId))
				.build();
	}
	
	/**
	 * @Description: 增加或者修改交接单 
	 * @Title: addConstructionSucceed 
	 * @param @param constructionSucceed
	 * @param @param bindingResult
	 * @param @param request
	 * @param @return    设定文件 
	 * @return ReturnCode    返回类型 
	 * @throws
	 */
	@PostMapping("succeed/setting")
	public ReturnCode updateOrAddConstructionSucceed(@RequestBody @Valid ConstructionSucceed constructionSucceed, BindingResult bindingResult, HttpServletRequest request) {
		if(bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error("新增流程参数异常："+msg);
			return new ReturnCode.Builder()
					.Success(false).Msg(msg).build();
		}
		return new ReturnCode.Builder()
				.Object(constructionSucceedService.updateOrAdd(constructionSucceed, ConstructionUtilService.getUserByRequest(request)))
				.build();
	}
	
	/**
	 * @Description: 根据项目ID展示今日/历史流程
	 * @Title: showWholeFlowByFlowId 
	 * @param @param flowId
	 * @param @return    设定文件 
	 * @return ReturnCode    返回类型 
	 * @throws
	 */
	@PostMapping("flow/todayandhistory/show")
	public ReturnCode showFlowTodayHistory(@RequestBody @Valid HistoryQueryBean historyQueryBean, BindingResult bindingResult){
		if(bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error("根据项目ID展示今日/历史流程参数异常："+msg);
			return new ReturnCode.Builder()
					.Success(false).Msg(msg).build();
		}
		return new ReturnCode.Builder()
				.Object(constructionBaseService.getTodayAndHistory(historyQueryBean))
				.build();
	}

	/**
	 * @Description: 根据项目ID展示今日/历史交接单
	 * @Title: showSucceedTodayHistory 
	 * @param @param projectId
	 * @param @return    设定文件 
	 * @return ReturnCode    返回类型 
	 * @throws
	 */
	@PostMapping("succeed/todayandhistory/show")
	public ReturnCode showSucceedTodayHistory(@RequestBody @Valid HistoryQueryBean historyQueryBean, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error("根据项目ID展示今日/历史交接单流程参数异常："+msg);
			return new ReturnCode.Builder()
					.Success(false).Msg(msg).build();
		}
		return new ReturnCode.Builder()
				.Object(constructionSucceedService.getTodayAndHistory(historyQueryBean))
				.build();
	}
	
	/**
	 * @Description: 根据交接单编号查询交接单信息
	 * @Title: showSucceedById 
	 * @param @param id
	 * @param @return    设定文件 
	 * @return ReturnCode    返回类型 
	 * @throws
	 */
	@GetMapping("succeed/info/{id}")
	public ReturnCode showSucceedById(@PathVariable("id") Integer id) {
		return new ReturnCode.Builder()
				.Object(constructionSucceedService.showInfoById(id))
				.build();
	}
	
	/**
	 * @Description: 根据指定流程编号展示今日流程
	 * @Title: hasTodayFlowByProject 
	 * @param @param project
	 * @param @return    设定文件 
	 * @return ReturnCode    返回类型 
	 * @throws
	 */
	@GetMapping("show/todayflow/{projectId}")
	public ReturnCode hasTodayFlowByProject(@PathVariable("projectId") Integer project) {
		return new ReturnCode.Builder()
				.Object(constructionBaseService.getTodayFlowByProject(project))
				.build();
	}

	@GetMapping("add/permission")
	public ReturnCode checkAddPermission(HttpServletRequest request){
		//判断是不是施工单位（只有施工单位可以新建流程）
		boolean isExePersons = constructionBaseService.checkExePersons(request);
		if(!isExePersons) {
			return new ReturnCode.Builder().Success(true).Msg("只有施工单位可以新建流程！").Object(isExePersons).build();
		}else{
			return new ReturnCode.Builder().Success(true).Msg("该用户有新建流程的权限！").Object(isExePersons).build();
		}
	}

	
}
